using System;
using System.Collections.Generic;

[Serializable]
public class SaveDataList<TData, TObject> where TData : SaveData, new() where TObject : ISavable
{
    public List<TData> datas;

    public SaveDataList()
    {
        datas = new List<TData>();
    }

    public TData Find(string identifier)
    {
        return datas.Find(x => x.identifier == identifier);
    }

    public void Save(TObject obj)
    {
        TData data = Find(obj.Identifier);
        if (data == null)
        {
            data = new TData();
            datas.Add(data);
        }
        data.Save(obj);
    }

    public void Load(TObject obj)
    {
        TData data = Find(obj.Identifier);
        if (data != null)
            obj.Load(data);
    }
}

[Serializable]
public class SaveData
{
    public string identifier;

    public virtual void Save(ISavable obj)
    {
        identifier = obj.Identifier;
    }
}